<?php

namespace app\admin\controller;

use app\admin\controller\Utils\FileImport;
use app\app\model\ProjectModel;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;

class MaterialInventoryList extends BaseController
{
    public function import()
    {
        // 获取表单上传文件
        if (empty(request()->file('file'))) {
            $this->error("请上传文件！");
        }

        try {
            $file = request()->file();
        } catch (\Exception $e) {
            $this->error("文件上传失败");
        }

        try {
            // 验证文件大小，名称等是否正确
            validate(['file' => 'filesize:52428800|fileExt:xls,xlsx'])
                ->check($file);
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }

        $file_path = root_path('runtime') . 'storage/';
        $file_name = Filesystem::putFile(date('Ymd') . '/' . 'zrr', $file['file']);

        $data = (new FileImport())->ExcelImport($file_path . $file_name, 0);

        $insert_array = [];
        $error_array = [];
        $i = 1;

        foreach ($data as $k => $v) {
            $i++;

            if ($k == 0) continue;

            $project = ProjectModel::where('project_name', $v[5])
                ->where('is_parent', 1)
                ->field(['id', 'project_name'])
                ->find();
//            if (empty($project)) {
//                $error_array[] = '第' . $i .  '行退料单体工程异常';
//                continue;
//            }

            $status = [
                '不可利库' => 2,
                '不可利库，建议报废' => 2,
                '不可利库，有账无物' => 2,
                '待核实' => 4,
                '可利库' => 1,
                '已盘点，建议报废' => 2,
                '已盘点，可利库' => 1,
                '已盘点,有争议' => 2,
                '已使用未入账' => 3,
                '已摊派' => 3,
            ];

            $insert_array[] = [
                'project_id' => $project->id ?? null,
                'project_name' => $v[4] ?? '',
                'children_project_name' => $v[5] ?? '',
                'number' => $v[1] ?? '',
                'audit_risk' => '',
                'describe' => $v[11] ?? '',
                'supplier' => $v[15] ?? '',
                'status' => $status[$v[2] ?? ''] ?? '',
                'liku_feasibility' => $v[3] ?? '',
                'charge_person' => $v[6] ?? '',
                'big_cate_des' => $v[7] ?? '',
                'mid_cate_des' => $v[8] ?? '',
                'small_cate_des' => $v[9] ?? '',
                'measure_unit' => $v[13] ?? '',
                'erp_num' => $v[14] ?? '',
                'erp_money' => $v[15] ?? '',
                'actual_storage_place' => $v[17] ?? '',
                'storage_class' => $v[18] ?? '',
                'link_person' => $v[19] ?? '',
                'link_peron_tel' => $v[20] ?? '',
            ];
        }

        if (!empty($error_array)) {
            $error_msg = implode('，', $error_array);
            $this->error('导入失败：' . $error_msg);
        }

        try {
            Db::table('t_material_inventory_list')->insertALl($insert_array);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('导入成功');
    }
}